package com.hosp.service;

import java.util.List;
import java.util.Objects;
import java.util.UUID;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hosp.bean.Doctor;
import com.hosp.bean.DoctorExample;
import com.hosp.mapper.DoctorMapper;
import com.hosp.vo.PageRequestVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * @author ztranscript 2021/10/02 1:07
 **/
@Slf4j
@Service
public class DoctorService {
    @Autowired
    private DoctorMapper doctorMapper;

    /**
     * 查询列表
     *
     * @param doctor      查询条件
     * @param pageRequest 分页条件
     * @return 响应
     */
    public PageInfo<Doctor> list(Doctor doctor, PageRequestVO pageRequest) {
        DoctorExample example = new DoctorExample();
        DoctorExample.Criteria criteria = example.createCriteria();
        String num = doctor.getdNum();
        if (!StringUtils.isEmpty(num)) {
            criteria.andDNumLike("%" + num + "%");
        }
        String name = doctor.getdName();
        if (!StringUtils.isEmpty(name)) {
            criteria.andDNameLike("%" + name + "%");
        }
        String keshi = doctor.getdKeshi();
        if (!StringUtils.isEmpty(keshi)) {
            criteria.andDKeshiLike("%" + keshi + "%");
        }

        // 分页查询
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        List<Doctor> doctors = doctorMapper.selectByExample(example);
        return new PageInfo<>(doctors);
    }

    /**
     * 保存
     *
     * @param doctor 保存对象
     * @return 响应
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean save(Doctor doctor) {
        if (Objects.isNull(doctor.getdId())) {
            // 生成随机编号
            String uuid = UUID.randomUUID().toString().substring(0, 6);
            doctor.setdNum("D-" + uuid);
            doctor.setdIntime(DateTime.now());
            doctorMapper.insertSelective(doctor);
        } else {
            doctorMapper.updateByPrimaryKey(doctor);
        }
        return true;
    }

    /**
     * 主键查询
     *
     * @param dId 查询条件
     * @return 响应
     */
    public Doctor selectById(Integer dId) {
        return doctorMapper.selectByPrimaryKey(dId);
    }

    /**
     * 删除
     *
     * @param uIds 待删除 IDs
     * @return 响应
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(List<Integer> uIds) {
        return true;
    }

    /**
     * 根据科室查询医生列表
     *
     * @param keshi 查询条件
     * @return 响应
     */
    public List<Doctor> selectByKs(String keshi) {
        DoctorExample example = new DoctorExample();
        example.createCriteria().andDKeshiEqualTo(keshi);
        List<Doctor> doctors = doctorMapper.selectByExample(example);
        return doctors;
    }
}
